package n1062;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	static int cost[][] =new int[101][101];
	
	static int M,N;
	
	static int dist[]=new int[101];
	
	static int level[]=new int[101];
	
	static int INF=Integer.MAX_VALUE;
	
	static int low,high,min=INF;
	
	static boolean valid[]=new boolean[101];
	
	
	/**
	 * 用深度优先搜索
	 * @param p
	 * @param cnt
	 */
	static void solve(int p,int cnt)
	{
		if(cnt>min)return;
		if(p==1) {
			if(cnt<min)min=cnt;
			return;
		}
		for(int i=1;i<=N;i++)
		{
			if(cost[p][i]!=INF) {
				if(low!=0)
				{
					if(Math.abs(level[i]-low)<=M&&Math.abs(level[i]-high)<=M)
					{
						
						int tl=low,th=high;
						low=Math.min(low, level[i]);
						high=Math.max(high, level[i]);
						
						solve(i,cnt+cost[p][i]);
						
						low=tl;high=th;
					}
				}else {
					low=high=level[i];
					
					solve(i,cnt+cost[p][i]);
					
					low=high=0;
				}
			}
		}
	}
	/**
	 * 或者用修改的迪杰斯特拉
	 * @return
	 */
	static int dijkstra()
	{
		boolean flag[] =new boolean[N+1];
		for(int i=0;i<=N;i++)dist[i]=INF;
		dist[0]=0;
		valid[0]=true;
		for(int i=0;i<=N;i++)
		{
			int min=INF,index=-1;
			for(int j=0;j<=N;j++)
			{
				if(valid[j]&&flag[j]==false&&dist[j]<min)
				{
					index=j;
					min=dist[j];
				}
			}
			if(index==-1)return dist[1];
			flag[index]=true;
			for(int j=0;j<=N;j++)
			{
				if(valid[j]&&flag[j]==false&&cost[index][j]!=INF&&dist[j]>dist[index]+cost[index][j])
				{
					dist[j]=dist[index]+cost[index][j];
				}
			}
		}
		return dist[1];
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		M=Integer.parseInt(st.nextToken());
		N=Integer.parseInt(st.nextToken());
		for(int i=0;i<=N;i++)
			for(int j=0;j<=N;j++)
			{
				cost[i][j]=INF;
			}
		
		
		for(int i=0;i<N;i++)
		{
			st=new StringTokenizer(bf.readLine());
			cost[0][i+1]=Integer.parseInt(st.nextToken());
			level[i+1]=Integer.parseInt(st.nextToken());
			int t=Integer.parseInt(st.nextToken());
			for(int k=0;k<t;k++)
			{
				st=new StringTokenizer(bf.readLine());
				int n1=Integer.parseInt(st.nextToken());
				int n2=Integer.parseInt(st.nextToken());
				cost[n1][i+1]=n2;
			}
		}
		//按等级来选择结点
		for(int i=level[1]-M;i<=level[1];i++)
		{
			for(int j=1;j<=N;j++)valid[j]=false;
			int end=i+M;
			for(int j=1;j<=N;j++)
			{
				if(level[j]>=i&&level[j]<=end)valid[j]=true;
			}
			int cur=dijkstra();
			if(min>cur)min=cur;
		}
		System.out.println(min);
	}

}
